package com.open.cloud.rbac.controller.orgpost;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.inspur.micro.core.response.ResponseWrapper;
import com.open.cloud.rbac.entity.orgpost.CoreCompany;
import com.open.cloud.rbac.object.orgpost.CoreCompanyDto;
import com.open.cloud.rbac.object.orgpost.CoreCompanySelectDto;
import com.open.cloud.rbac.object.orgpost.CoreCompanyVo;
import com.open.cloud.rbac.service.orgpost.ICoreCompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 公司表 前端控制器
 * </p>
 *
 * @author wangzengzhou01
 * @since 2024-12-23
 */
@Api(tags = "RBAC服务-公司模块业务")
@Slf4j
@RestController
@RequestMapping(value = "api/v1/core/company")
public class CoreCompanyController {

    @Resource
    private ICoreCompanyService companyService;

    @ApiOperation(value = "创建")
    @PostMapping("/add")
    public ResponseWrapper addCompanyInfo(@RequestBody CoreCompanyDto obj) {
        JSONObject result = companyService.addCompanyInfo(obj);
        String message = result.getString("message");
        Integer code = result.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

    @ApiOperation(value = "更新")
    @PostMapping("/update")
    public ResponseWrapper updateCompanyInfo(@RequestBody CoreCompanyDto obj) {
        JSONObject result = companyService.updateCompanyInfo(obj);
        String message = result.getString("message");
        Integer code = result.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

    @ApiOperation(value = "按ID删除")
    @GetMapping("/delete")
    public ResponseWrapper deleteCompanyInfo(@RequestParam(name = "id") String id) {
        JSONObject result = companyService.deleteCompanyInfo(id);
        String message = result.getString("message");
        Integer code = result.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

    @ApiOperation(value = "分页查询")
    @PostMapping("/page")
    public ResponseWrapper<IPage<CoreCompany>> selectCompanyPage(@RequestBody CoreCompanySelectDto companySelectDto) {
        IPage<CoreCompany> data = companyService.selectCompanyPage(companySelectDto);
        return ResponseWrapper.success(data);
    }

    @ApiOperation(value = "查询列表")
    @GetMapping("/list")
    public ResponseWrapper<List<CoreCompany>> selectCompanyList() {
        List<CoreCompany> data = companyService.selectCompanyList();
        return ResponseWrapper.success(data);
    }

    @ApiOperation(value = "查询公司部门结构树")
    @GetMapping("/tree")
    public ResponseWrapper<List<CoreCompanyVo>> selectCompanyOrgTree() {
        List<CoreCompanyVo> data = companyService.selectCompanyOrgTree();
        return ResponseWrapper.success(data);
    }

    @ApiOperation(value = "查询公司详情信息")
    @GetMapping("/detail")
    public ResponseWrapper<CoreCompany> getCompanyDetailById(@RequestParam("companyId") String companyId) {
        CoreCompany data = companyService.getCompanyDetailById(companyId);
        return ResponseWrapper.success(data);
    }

}

